#include <string>
#include "FunctionEntry.h"

FunctionEntry::FunctionEntry() : TableEntry(FUNCTION) 
{
}

FunctionEntry::~FunctionEntry()
{
	clearParameterList();
}

void FunctionEntry::setLabel(std::string pLabel) {
	label = pLabel;
}

std::string FunctionEntry::getLabel() const {
	return label;
}

void FunctionEntry::setReturnType(Type type) {
	if(type == T_ERROR) {
		// oops
	}
	returnType = type;
}

Type FunctionEntry::getReturnType() const {
	return returnType;
}

void FunctionEntry::setParameterList(ParamList* pl) {
	parameterList=pl;
	if(!parameterList) return;
	for(ParamList::const_iterator i=parameterList->begin();
		i!=parameterList->end();
		++i)
	{
		
		(*i)->setParamFlag(); 
	}
}

ParamList* FunctionEntry::getParamaterList() {
	return parameterList;
}

int FunctionEntry::getParamCount() const {
	return parameterList->size();
}

attr FunctionEntry::getAttr() const {
	attr retVal;
	retVal.type = returnType;
	retVal.addr = getAddress();
	retVal.attrib = VA_SIMPLE;
	return retVal;
}

void FunctionEntry::clearParameterList() {
	for(ParamList::const_iterator i=parameterList->begin();
		i!=parameterList->end();
		++i)
	{
		delete(*i);
	}
	parameterList->clear();
}

std::ostream& FunctionEntry::inserter(std::ostream &os) const {
	std::string retTypeStr;
	switch(returnType) {
	case T_VOID:
		retTypeStr="void";
		break;
	case T_INT:
		retTypeStr="int";
		break;
	case T_CHAR:
		retTypeStr="char";
		break;
	default:
		retTypeStr="unknown";
	}
	
	os << "function returning '" << retTypeStr << "', with " << parameterList->size() << " parameters";
	
	for(ParamList::const_iterator i=parameterList->begin();
		i!=parameterList->end();
		++i) 
	{
		if(i==parameterList->begin()) {
			os << ":";
		}
		os << "\n\t" << *(*i); 
	}
	
	return os;
}

